package com.ycfmedical.bed.ui;

import android.app.Activity;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.listener.IvsReplaceData;
import com.ycfmedical.bed.model.ChartManage;
import com.ycfmedical.bed.base.BaseMenuActivity;
import com.ycfmedical.bed.service.VitalSignService;
import com.ycfmedical.bed.utils.ResourceUtil;

import java.util.ArrayList;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.router.Router;

public class VitalSignActivity extends BaseMenuActivity implements IvsReplaceData {

    @BindView(R.id.tab_heart)
    LineChart heartLineChart;               //心率

    @BindView(R.id.tab_temperature)
    LineChart temperatureLineChart;         //呼吸

    @BindView(R.id.tab_breathe)
    LineChart breatheLineChart;             //体温

    @BindView(R.id.tab_sleep)
    LineChart testLineChart;                //睡眠


    @BindView(R.id.text_heart)
    TextView heart;                         //心率

    @BindView(R.id.text_breathe)
    TextView breathe;                       //呼吸

    @BindView(R.id.text_temperature)
    TextView temperature;                   //体温

    private ChartManage chartManage;        //图表管理类，用来设置图表样式
    private ArrayList<TextView> textViews;
    private VitalSignService.Mybinder mybinder;
    private ArrayList<Integer> xItemMax;

    private ArrayList<LineChart> linecharts;
    private ArrayList<LineData> lindatas;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
//            ((VitalSignService.Mybinder) service).setIvsReplacedata(VitalSignActivity.this);
            mybinder = (VitalSignService.Mybinder) service;

            linecharts = chartManage.getLineCharts();
            for (int i = 0; i < linecharts.size(); i++) {
                lindatas.add(linecharts.get(i).getLineData());
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            //Service被停止，回调的方法
        }
    };

    @Override
    public void initData(Bundle savedInstanceState) {
        chartManage = new ChartManage();
        init();
        /**
         * 绑定Service获得数据,必须先初始化，再绑定Service
         * */
//        Intent intent = new Intent(VitalSignActivity.this, VitalSignService.class);
//        bindService(intent, connection, Context.BIND_AUTO_CREATE);
    }

    public void init() {
        chartManage.addChart(heartLineChart);
        chartManage.addChart(temperatureLineChart);
        chartManage.addChart(breatheLineChart);
        chartManage.addChart(testLineChart);

        lindatas = new ArrayList<LineData>();
        textViews = new ArrayList<TextView>();
        xItemMax = new ArrayList<>();

//        chartManage.setX(0,0f, Constants.Y_TEST_CONTENT , false);
//        chartManage.setX(1,0f, Constants.Y_TEST_CONTENT, false);
//        chartManage.setX(2,0f, Constants.Y_TEST_CONTENT , false);
//        chartManage.setX(3,0f, Constants.Y_TEST_CONTENT, false);
//
//        chartManage.setY(0,0f, Constants.Y_TEST_CONTENT, false);
//        chartManage.setY(1,0f, Constants.Y_TEST_CONTENT, false);
//        chartManage.setY(2,0f, Constants.Y_TEST_CONTENT, false);
//        chartManage.setY(3,0f, Constants.Y_TEST_CONTENT, false);

        textViews.add(heart);
        textViews.add(breathe);
        textViews.add(temperature);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_vital_sign;
    }

    @Override
    public XPresent newP() {
        return null;
    }

    public static void launch(Activity activity) {
        Router.newIntent(activity).to(VitalSignActivity.class).launch();

    }

    @Override
    protected void initToolbar() {
        super.initToolbar();
        getToolbarTitle().setText(ResourceUtil.getResources().getString(R.string.health_vs));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        chartManage = null;
        mybinder.onUnbind();
        unbindService(connection);
    }


    @Override
    public void replaceData( int content) {
        for (int i = 0; i < content; i++) {
            linecharts.get(i).notifyDataSetChanged();
            linecharts.get(i).invalidate();
        }
    }

    @Override
    public void setServiceData(ArrayList<ArrayList<Entry>> lists) {
        chartManage.setLineData(lists);
        chartManage.setLine(0,Color.RED, false, LineDataSet.Mode.HORIZONTAL_BEZIER);
        chartManage.setLine(1,Color.WHITE, false, LineDataSet.Mode.CUBIC_BEZIER);


        chartManage.setbackground(0,Color.YELLOW);
        chartManage.setbackground(1,Color.BLUE);
    }


    @Override
    public void setText(ArrayList<Float> data) {
        for(int i = 0;i<textViews.size();i++){
            textViews.get(i).setText(data.get(i)+"");
        }
    }

    @Override
    public void setText(int flag, String data) {

    }

}
